import java.io.*;

public class sparseArrayIO {

    public static void main(String[] args) {
        //原始二维数组11*11
        int chessArr1[][] = new int[11][11];
        //1代表黑子，2代表蓝子
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        //遍历二维数组
        System.out.println("原始二维数组");
        for (int[] ints : chessArr1) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

        //将二维数组转换为稀疏数组
        //1.先遍历二维数组，得到非0的数据个数。
        int count = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1.length; j++) {
                //不为零代表有棋子
                if (chessArr1[i][j] != 0) count++;
            }
        }
        //2.创建对应的稀疏数组,存放的数据个数+1行，列为固定3列即可，分别记录row，column，value
        int[][] sparseArr = new int[count + 1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        sparseArr[0][2] = count;

        //2.给稀疏数组赋值
        int sum = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                    sparseArr[sum][0] = i;//存放原数组中row
                    sparseArr[sum][1] = j;//存放原数组中column
                    sparseArr[sum][2] = chessArr1[i][j];//存放原数组中该位置的value值
                }
            }
        }

        FileOutputStream fileOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(new File("chess.data"));
            //遍历稀疏数组
            System.out.println();
            System.out.println("稀疏数组");
            for (int i = 0; i < sparseArr.length; i++) {
                for (int j = 0; j < sparseArr[i].length; j++) {
                    fileOutputStream.write(sparseArr[i][j]);
                    System.out.print(sparseArr[i][j] + "\t");
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        FileInputStream fileInputStream = null;
        int sparseArr2[][] = null;
        try {
            //将稀疏数组从磁盘的文件中读取出来
            fileInputStream = new FileInputStream(new File("chess.data"));
            sparseArr2 = new int[count + 1][3];
            for (int i = 0; i < sparseArr2.length; i++) {
                for (int j = 0; j < sparseArr2[i].length; j++) {
                    sparseArr2[i][j] = fileInputStream.read();
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println();
        System.out.println("从磁盘中读取的稀疏数组");
        for (int[] is : sparseArr2) {
            for (int i : is) {
                System.out.print(i + "\t");

            }
            System.out.println();
        }
        System.out.println();


        //将稀疏数组转换为二维数组
        //1.得到稀疏数组中的第一行存放有关二维数组的值
        int row = sparseArr[0][0];//原二维数组的行
        int column = sparseArr[0][1];//原二维数组的列
        int value = sparseArr[0][2];//原二维数组的存放的值个数

        int[][] chessArr2 = new int[row][column];
        //遍历稀疏数组存放的值，将其按对应顺序存放到二维数组中。没赋值的初始化默认为0
        for (int i = 1; i < sparseArr.length; i++) {
            for (int j = 1; j < sparseArr[i].length; j++) {
                //遍历稀疏数组存放的值，将其按对应顺序存放到二维数组中
                row = sparseArr[i][0];
                column = sparseArr[i][1];
                value = sparseArr[i][2];
                chessArr2[row][column] = value;
            }
        }

        //遍历根据稀疏数组转换的二维数组
        System.out.println();
        System.out.println("新二维数组");
        for (int[] ints : chessArr2) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }
}
